package com.yorick.file.service.impl;

import com.mysql.cj.log.Log;
import com.yorick.common.Result.SimpleFactory.ResultFactory;
import com.yorick.common.error.YorickAssert;
import com.yorick.file.dao.FileMapper;
import com.yorick.file.entity.DeleteFileDto;
import com.yorick.file.entity.FileDto;
import com.yorick.file.entity.KeywordFileDto;
import com.yorick.file.modal.request.*;
import com.yorick.file.modal.response.FileDetailResponse;
import com.yorick.file.service.FileService;
import com.yorick.login.dao.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    FileMapper fileMapper;
    @Autowired
    LoginMapper loginMapper;

    @Override
    public Map<String, Object> createFile(CreateFileRequest requestBody, int aid) {
        Integer isPrivate = requestBody.getParentId() == 0 ? 1 : fileMapper.isPrivate(requestBody.getParentId());

        FileDto data = FileDto.builder()
                .fileName(requestBody.getFileName())
                .parentId(requestBody.getParentId())
                .fileType(requestBody.getFileType())
                .fileSize(requestBody.getFileSize())
                .fileUrl(requestBody.getFileUrl())
                .isPrivate(isPrivate)
                .aid(aid)
                .build();

        Integer isFolderExist = fileMapper.isFileExist(data);

        YorickAssert.validate(
                isFolderExist == 0,
                () -> "0",
                () -> "该文件夹或文件已存在");

        fileMapper.insertFile(data);
        if (isPrivate == 1) {
            fileMapper.insertAccountFile(data);
        }
        return ResultFactory.success(data.getId()).display();
    }

    @Override
    public Map<String, Object> fileList(FileListRequest requestBody, int aid) {
        FileDto data = FileDto.builder()
                .aid(aid)
                .parentId(requestBody.getParentId())
                .build();
        List<FileDto> resultList = fileMapper.findPublicFileList(data);
        resultList.addAll(fileMapper.findFileList(data));
        return ResultFactory.success(resultList).display();
    }

    @Override
    public Map<String, Object> searchFile(SearchFileRequest requestBody, int aid) {
        KeywordFileDto data = KeywordFileDto.builder()
                .aid(aid)
                .keyword(requestBody.getKeyword())
                .build();
        List<FileDto> resultList = fileMapper.findPublicFileByKeyword(data);
        resultList.addAll(fileMapper.findFileByKeyword(data));
        return ResultFactory.success(resultList).display();
    }

    @Override
    public Map<String, Object> fileDetail(FileDetailRequest requestBody, int aid) {
        FileDto data = FileDto.builder()
                .aid(aid)
                .id(requestBody.getId())
                .build();
        FileDto file = fileMapper.findFileById(data);
        YorickAssert.validate(
                Objects.nonNull(file),
                () -> "0",
                () -> "找不到该文件");

        FileDetailResponse response = FileDetailResponse.builder()
                .fileName(file.getFileName())
                .id(file.getId())
                .parentId(file.getParentId())
                .fileType(file.getFileType())
                .fileSize(file.getFileSize())
                .lastTime(file.getLastTime())
                .userName(requestBody.getId() < 0 ? "系统" : loginMapper.getAccountByAid(file.getAid()).getUsername())
                .isPrivate(file.getIsPrivate())
                .build();

        return ResultFactory.success(response).display();
    }

    @Override
    public Map<String, Object> fileRename(FileRenameRequest requestBody, int aid) {
        FileDto data = FileDto.builder()
                .aid(aid)
                .id(requestBody.getId())
                .parentId(requestBody.getParentId())
                .fileName(requestBody.getName())
                .build();
        YorickAssert.validate(
                requestBody.getId() != -1,
                () -> "0",
                () -> "不能修改public文件夹");
        Integer isFolderExist = fileMapper.isFileExist(data);
        YorickAssert.validate(
                isFolderExist == 0,
                () -> "0",
                () -> "该文件夹或文件已存在");
        FileDto file = fileMapper.findFileById(data);
        YorickAssert.validate(
                Objects.nonNull(file),
                () -> "0",
                () -> "找不到该文件");
        fileMapper.updateFile(data);
        return ResultFactory.success().display();
    }

    @Override
    public Map<String, Object> deleteFile(FileDetailRequest requestBody, int aid) {
        FileDto data = FileDto.builder()
                .aid(aid)
                .id(requestBody.getId())
                .build();
        YorickAssert.validate(
                requestBody.getId() != -1,
                () -> "0",
                () -> "不能删除public文件夹");
        FileDto file = fileMapper.findFileById(data);
        List<Integer> childList = fileMapper.findFileChildList(data);
        DeleteFileDto data2 = DeleteFileDto.builder()
                .aid(aid)
                .fileIds(childList)
                .enabled(0)
                .build();
        if (file.getIsPrivate() == 0) {
            fileMapper.deletePublicFile(data2);
        } else {
            fileMapper.updateFileStatus(data2);
            fileMapper.updateFileLastTime(data2);
        }
        return ResultFactory.success().display();
    }
}
